package crawler.douban;

import constant.DBConstant;
import crawler.http.HttpClientTest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * 豆瓣电视剧列表爬取
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-09-19 14:27
 */
public class DBTVListCatch implements Runnable {

    int num = 0;

    @Override
    public void run() {
        Elements elements = new Elements();
        while (num == 0 ||
                (num != 0 && elements.size() > 0)) {
            String url = DBConstant.DB_MOVIE_LIST_URL.replace("START_NUM", num + "");
            Document doc = null;
            try {
                doc = Jsoup.connect(url).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            elements = doc.select(".doulist-item");
            for (Element element : elements) {
                System.out.println(Teleplay.newTeleplay(element));
//                DouBanSpider.waitList.add(Teleplay.newTeleplay(element));
            }
            num = num + elements.size();
            System.out.println(url + ": (起始位置) " + num + ": (获取)" + elements.size() + ": (待抓取)" + DouBanSpider.waitList.size());
            DouBanSpider.friendlyToDouban(10);
        }
    }

    public static void main(String[] args) {
        DBTVListCatch listCatch = new DBTVListCatch();
        new Thread(listCatch).start();
    }
}
